package com.util;

import java.util.Date;
import java.text.SimpleDateFormat;
import java.text.ParseException;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.StringTokenizer;

public abstract class DateUtils {

	private static DateFormat dateTimeFormatter = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");

	private static DateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");

	private static final long MSECONDS_OF_ONE_DAY = 60 * 60 * 1000 * 24;

	public static Date getCurrentDate() {
		return new Date();
	}

	public static String getDateString(Date date) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		return df.format(date);
	}
	
	public static String getDateStringMe(Date date) {
		SimpleDateFormat df;
		if(date.getMonth() > 9 && date.getDay() > 9){
			df = new SimpleDateFormat("yy-MM-dd");
		}else if(date.getMonth() > 9 && date.getDay() <= 9){
			df = new SimpleDateFormat("yy-MM-d");	
		}else if(date.getMonth() <= 9 && date.getDay() > 9){
			df = new SimpleDateFormat("yy-M-dd");	
		}else{
			df = new SimpleDateFormat("yy-M-d");	
		}
		return df.format(date);
	}
	
	public static String getDateMonthString(Date date) {
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
		return df.format(date);
	}

	public static String getDateString(Date date, String format) {
		SimpleDateFormat df = new SimpleDateFormat(format);
		return df.format(date);
	}

	public static String getDateTimeString(Date date) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return df.format(date);
	}
	/**
	 * 
	 * <p>功能描述：生成图片名称用</p>
	 * @param date
	 * @return
	 * @author:Administrator
	 * @update:[日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	public static String getDateTimeString2(Date date) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
		return df.format(date);
	}
	
	public static String getDateTimeStringMe(Date date) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		return df.format(date);
	}
	
	public static String getDateToString(Date date) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		return df.format(date);
	}
	
	public static String getStringDate(Date date) {
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMDDHHmmss");
		return df.format(date);
	}
	/**
	 * Ϊ��Ǯ2.0 �ӿڣ��������ʽת����
	 * @param date
	 * @return
	 */
	public static String getStringDateBill(Date date) {
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddhhmmss");
		return df.format(date);
	}
	public static String getDateTimeString(Date date, String format) {
		SimpleDateFormat df = new SimpleDateFormat(format);
		return df.format(date);
	}

	public static Date parseDate(String date) throws ParseException {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		return df.parse(date);
	}

	public static Date parseDate(String date, String format)
			throws ParseException {
		SimpleDateFormat df = new SimpleDateFormat(format);
		return df.parse(date);
	}

	public static Date parseDateTime(String date) throws ParseException {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return df.parse(date);
	}
	//write by zhuyiqi 2007-05-16
	public static Date parseDateTimeMe(String date) throws ParseException {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		return df.parse(date);
	}
	public static Date parseDateTime(String date, String format)
			throws ParseException {
		SimpleDateFormat df = new SimpleDateFormat(format);
		return df.parse(date);
	}

	// write by steven.shi 2004-10-18
	public static Date getFirstDayOfMonth(int year, int month) {
		Calendar cl = Calendar.getInstance();
		cl.set(Calendar.YEAR, year);
		cl.set(Calendar.MONTH, month - 1);
		cl.set(Calendar.DAY_OF_MONTH, 1);
		return cl.getTime();
	}

	public static Date getLastDayOfMonth(int year, int month) {
		Calendar cl = Calendar.getInstance();
		cl.set(Calendar.YEAR, year);
		cl.set(Calendar.MONTH, month - 1);
		cl.set(Calendar.DAY_OF_MONTH, cl
				.getActualMaximum(Calendar.DAY_OF_MONTH));
		return cl.getTime();
	}

	/*
	 * write by steven.shi 2004-10-18 orgDate format:YYYY-MM-DD
	 */
	public static Date convertOrgDate(String orgDate) {
		Calendar cl = Calendar.getInstance();
		cl.set(Calendar.YEAR, Integer.parseInt(orgDate.substring(0, 4)));
		cl.set(Calendar.MONTH, Integer.parseInt(orgDate.substring(5, 7)) - 1);
		cl.set(Calendar.DAY_OF_MONTH, Integer
				.parseInt(orgDate.substring(8, 10)));
		cl.set(Calendar.HOUR_OF_DAY, 0);
		cl.set(Calendar.MINUTE, 0);
		cl.set(Calendar.SECOND, 0);
		return cl.getTime();
	}

	/*
	 * write by yifei.leng 2007-06-19 orgDate format:YYYY-MM-DD
	 */
	public static Calendar toCalendar(String str) {
		SimpleDateFormat dateFormat = new SimpleDateFormat();
		Calendar cl=Calendar.getInstance();
		try {
			dateFormat.applyPattern("yyyy-MM-dd");
			Date vDate = dateFormat.parse(str);
			cl.setTime(vDate);
			return cl;
		} catch (ParseException e) {
			return null;
		}
	}
	
	
	/*
	 * write by steven.shi 2004-10-18 orgDate format:YYYY-MM-DD
	 */
	public static Date convertRealTimeDate(String orgDate) {
		Calendar cl = Calendar.getInstance();
		cl.set(Calendar.YEAR, Integer.parseInt(orgDate.substring(0, 4)));
		cl.set(Calendar.MONTH, Integer.parseInt(orgDate.substring(5, 7)) - 1);
		cl.set(Calendar.DAY_OF_MONTH, Integer
				.parseInt(orgDate.substring(8, 10)));
		return cl.getTime();
	}

	/*
	 * Get the Next Date Write by Jeffy pan 2004-10-21 Date Format:YYYY-MM-DD
	 * YYYY:M:D YYYY/M/DD
	 */
	public static String getNextDate(String date) {

		Calendar cd = Calendar.getInstance();
		StringTokenizer token = new StringTokenizer(date, "-/ :");
		if (token.hasMoreTokens()) {
			cd.set(Calendar.YEAR, Integer.parseInt(token.nextToken()));
		} else {
			cd.set(Calendar.YEAR, 1970);
		}
		if (token.hasMoreTokens()) {
			cd.set(Calendar.MONTH, Integer.parseInt(token.nextToken()) - 1);
		} else {
			cd.set(Calendar.MONTH, 0);
		}
		if (token.hasMoreTokens()) {
			cd.set(Calendar.DAY_OF_MONTH, Integer.parseInt(token.nextToken()));
		} else {
			cd.set(Calendar.DAY_OF_MONTH, 1);
		}
		if (token.hasMoreTokens()) {
			cd.set(Calendar.HOUR_OF_DAY, Integer.parseInt(token.nextToken()));
		} else {
			cd.set(Calendar.HOUR_OF_DAY, 0);
		}
		if (token.hasMoreTokens()) {
			cd.set(Calendar.MINUTE, Integer.parseInt(token.nextToken()));
		} else {
			cd.set(Calendar.MINUTE, 0);
		}
		if (token.hasMoreTokens()) {
			cd.set(Calendar.SECOND, Integer.parseInt(token.nextToken()));
		} else {
			cd.set(Calendar.SECOND, 0);
		}
		if (token.hasMoreTokens()) {
			cd.set(Calendar.MILLISECOND, Integer.parseInt(token.nextToken()));
		} else {
			cd.set(Calendar.MILLISECOND, 0);
		}

		long nextTime = cd.getTimeInMillis() + 24 * 60 * 60 * 1000;
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		return format.format(new Date(nextTime));
	}

	/*
	 * Get the Next Date Write by Jeffy pan 2004-10-21 Date Format:(YYYY-MM-DD)
	 * (YYYY:M:D HH:MM:SS) (YYYY/M/DD hh:MM)
	 */
	public static Date stringToDate(String date) {
		if (date == null)
			return null;

		Calendar cd = Calendar.getInstance();
		StringTokenizer token = new StringTokenizer(date, "-/ :");
		if (token.hasMoreTokens()) {
			cd.set(Calendar.YEAR, Integer.parseInt(token.nextToken()));
		} else {
			cd.set(Calendar.YEAR, 1970);
		}
		if (token.hasMoreTokens()) {
			cd.set(Calendar.MONTH, Integer.parseInt(token.nextToken()) - 1);
		} else {
			cd.set(Calendar.MONTH, 0);
		}
		if (token.hasMoreTokens()) {
			cd.set(Calendar.DAY_OF_MONTH, Integer.parseInt(token.nextToken()));
		} else {
			cd.set(Calendar.DAY_OF_MONTH, 1);
		}
		if (token.hasMoreTokens()) {
			cd.set(Calendar.HOUR_OF_DAY, Integer.parseInt(token.nextToken()));
		} else {
			cd.set(Calendar.HOUR_OF_DAY, 0);
		}
		if (token.hasMoreTokens()) {
			cd.set(Calendar.MINUTE, Integer.parseInt(token.nextToken()));
		} else {
			cd.set(Calendar.MINUTE, 0);
		}
		if (token.hasMoreTokens()) {
			cd.set(Calendar.SECOND, Integer.parseInt(token.nextToken()));
		} else {
			cd.set(Calendar.SECOND, 0);
		}
		if (token.hasMoreTokens()) {
			cd.set(Calendar.MILLISECOND, Integer.parseInt(token.nextToken()));
		} else {
			cd.set(Calendar.MILLISECOND, 0);
		}

		return cd.getTime();
	}

	/*
	 * Get the Next Date Write by Jeffy pan 2004-10-21 Date Format:(YYYY-MM-DD)
	 * (YYYY:M:D HH:MM:SS) (YYYY/M/DD hh:MM)
	 */
	public static String dateToString(Date date) {
		if (date == null)
			return "";
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		return formatter.format(date).trim();

	}

	public static String dateTimeToString(Date date) {
		if (date == null)
			return "";
		return dateTimeFormatter.format(date).trim();

	}

	/*
	 * Get the Next Date Write by Jeffy pan 2004-10-21 Date Format:(YYYY-MM-DD)
	 * (YYYY:M:D HH:MM:SS) (YYYY/M/DD hh:MM)
	 */
	public static String dateToString(Date date, String format) {

		SimpleDateFormat formatter = new SimpleDateFormat(format);
		return formatter.format(date).trim();
	}

	/*
	 * Get the Next Date Write by Jeffy pan 2004-10-21 Date Format:(YYYY-MM-DD)
	 * (YYYY:M:D HH:MM:SS) (YYYY/M/DD hh:MM)
	 */
	public static int getDays(String fromDate, String endDate) {

		long from = stringToDate(fromDate).getTime();
		long end = stringToDate(endDate).getTime();

		return (int) ((end - from) / (24 * 60 * 60 * 1000)) + 1;
	}

	public static int getDays(Date fromDate, Date endDate) {

		long from = fromDate.getTime();
		long end = endDate.getTime();

		return (int) ((end - from) / (24 * 60 * 60 * 1000)) + 1;
	}

	public static String getTakeTime(Date startDate, Date endDate) {
		int minute = 0;
		try {
			minute = (int) (endDate.getTime() - startDate.getTime())
					/ (1000 * 60);
			return String.valueOf(minute);
		} catch (Exception e) {
			return "";
		}

	}

	/*
	 * ��ȡ�·ݵĵ�һ�� written by Sammy: 2004-10-26
	 */
	public static int getFirstDateOfMonth(int year, int month) {

		Calendar cl = Calendar.getInstance();
		cl.set(Calendar.YEAR, year);
		cl.set(Calendar.MONDAY, month - 1);
		return cl.getActualMinimum(Calendar.DAY_OF_MONTH);
	}

	/*
	 * ��ȡ�·ݵ����һ�� written by Sammy: 2004-10-26
	 */
	public static int getLastDateOfMont(int year, int month) {
		Calendar cl = Calendar.getInstance();
		cl.set(Calendar.YEAR, year);
		cl.set(Calendar.MONDAY, month - 1);
		return cl.getActualMaximum(Calendar.DAY_OF_MONTH);
	}

	public static java.sql.Date convertUtilDateToSQLDate(java.util.Date date) {
		if (date == null)
			return null;
		Calendar cl = Calendar.getInstance();
		cl.setTime(date);
		java.sql.Date jd = new java.sql.Date(cl.getTimeInMillis());
		return jd;
	}
	
	public static java.sql.Date convertObjToSQLDate(Object obj) {
		if (obj==null || "".equals(obj.toString().trim()) )
			return null;

		Calendar cl = Calendar.getInstance();
		cl.setTime((java.util.Date)obj);
		java.sql.Date jd = new java.sql.Date(cl.getTimeInMillis());
		return jd;
	}

	public static java.sql.Timestamp convertUtilDateToSQLDateWithTime(
			java.util.Date date) {
		if (date != null) {
			return new java.sql.Timestamp(date.getTime());
		} else {
			return null;
		}
	}

	public static java.sql.Date convertStringToSQLDate(String dateString) {
		return (convertUtilDateToSQLDate(stringToDate(dateString)));
	}

	public static java.sql.Date convertToSQLDateWithoutTime(java.util.Date date) {
		String dateString = dateFormatter.format(date);
		return convertStringToSQLDate(dateString);
	}

	/**
	 * get offset to the previous sunday from the specific date
	 * 
	 * @param from
	 *            the specific start date
	 * @param from
	 *            the specific end date
	 * @return offset to the previous sunday
	 * @throws ParseException
	 */
	public static List getAllSundays(Date from, Date to) {
		List sundayList = new ArrayList();
		int offset = getOffsetToNextSunday(from);
		Date firstSunday = addDate(from, offset);
		Date current = firstSunday;
		while (current.compareTo(to) <= 0) {
			sundayList.add(current);
			current = addDate(current, 7);
		}
		return sundayList;
	}

	/**
	 * get offset to the next sunday from the specific date
	 * 
	 * @param date
	 *            the specific date
	 * @return offset to the next sunday
	 * @throws ParseException
	 */
	public static int getOffsetToNextSunday(Date date) {
		if (getDayOfWeek2(date) == 1)
			return 0;
		return 8 - getDayOfWeek2(date);
	}

	/**
	 * get day index of a week for the specific date
	 * 
	 * @param date
	 *            the specific date
	 * @return day index of a week,Mon. is 1,Tues. is 2,Wed. is 3,Thurs. is
	 *         4,Fri. is 5,Sat. is 6,Sun. is 7
	 * @throws ParseException
	 */
	public static int getDayOfWeek(Date date) {
		if (date == null)
			return 0;
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int result = calendar.get(Calendar.DAY_OF_WEEK) - 1;
		if (result == 0)
			result = 7;
		return result;
	}

	/**
	 * get day index of a week for the specific date
	 * 
	 * @param date
	 *            the specific date
	 * @return day index of a week,Sun. is 1,Mon. is 2,Tues. is 3,Wed. is
	 *         4,Thurs. is 5,Fri. is 6,Sat. is 7
	 * @throws ParseException
	 */
	public static int getDayOfWeek2(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.DAY_OF_WEEK);
	}

	/**
	 * add days to the specific date
	 * 
	 * @param SourceDate
	 *            the specific date
	 * @param days
	 *            day count to be added
	 * @return java.util.Date object after add days
	 * @throws ParseException
	 */
	public static Date addDate(Date sourceDate, int days) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(sourceDate);
		calendar.add(Calendar.DATE, days);
		return calendar.getTime();
	}

	/**
	 * add days to the specific date
	 * 
	 * @param SourceDate
	 *            the specific date
	 * @param days
	 *            day count to be added
	 * @return java.util.Date object after add days
	 * @throws ParseException
	 */
	public static Date addDate(String stringDate, int days) {
		Date sourceDate = stringToDate(stringDate);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(sourceDate);
		calendar.add(Calendar.DATE, days);
		return calendar.getTime();
	}

	/**
	 * @param stringDate
	 * @return
	 */
	public static String addOneDay(Date sourceDate) {

		Date newDate = addDate(sourceDate, 1);
		return dateToString(newDate);

	}

	/**
	 * 
	 * @param from
	 * @param to
	 * @return
	 * @throws ParseException
	 */
	public static long subDate(Date from, Date to) throws ParseException {
		long value = Math.abs(to.getTime() - from.getTime());
		return value / MSECONDS_OF_ONE_DAY;
	}

	/**
	 * 
	 * @param from
	 * @param to
	 * @return
	 * @throws ParseException
	 */
	public static long subDate(String from, String to) throws ParseException {
		return subDate(stringToDate(from), stringToDate(to));
	}

	/*
	 * ����ʱ���б� (startDate, endDate, days[])
	 */
	public static List getStringDateList(String startDate, String endDate,
			int[] days) {

		List dateList = new ArrayList();

		int days2 = DateUtils.getDays(startDate, endDate);
		Date fromDate2 = DateUtils.stringToDate(startDate);
		Date endDate2 = DateUtils.stringToDate(endDate);

		Calendar cal = Calendar.getInstance();

		for (int i = 0; i < days2; i++) {

			cal.setTime(fromDate2);
			cal.add(Calendar.DATE, i);

			for (int j = 0; j < days.length; j++) {
				// �����������ѡ��
				if (days[j] == cal.get(Calendar.DAY_OF_WEEK)) {

					dateList.add(DateUtils.dateToString(cal.getTime()));
				}// if
			}// for

		}// for

		return dateList;
	}

	public static List getDateList(String startDate, String endDate, int[] days) {

		List dateList = new ArrayList();

		int days2 = DateUtils.getDays(startDate, endDate);
		Date fromDate2 = DateUtils.stringToDate(startDate);
		Date endDate2 = DateUtils.stringToDate(endDate);

		Calendar cal = Calendar.getInstance();

		for (int i = 0; i < days2; i++) {

			cal.setTime(fromDate2);
			cal.add(Calendar.DATE, i);

			for (int j = 0; j < days.length; j++) {
				// �����������ѡ��
				if (days[j] == cal.get(Calendar.DAY_OF_WEEK)) {
					dateList.add(cal.getTime());
				}// if
			}// for

		}// for

		return dateList;
	}

	public static int compareDate(Date firstDate, Date secondDate) {
		return firstDate.compareTo(secondDate);
	}

	
}
